package ie.lawlor.amvc.example.explorer.login;

import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.patterns.standard.StandardController;
import ie.lawlor.amvc.swing.JPanelView;


public class LoginView extends JPanelView {
    
    public LoginView(String name) {
    	super(name);
        initComponents();
		setModal(true);
		setTitle("Log In");
    }
    
    public void doUpdateView(Event e) {
    	LoginModel model = (LoginModel)e.getPayload();
        messageLabel.setText(model.getMessage());
        usernameTF.setText(model.getUsername());
        passwordTF.setText(model.getPassword());
    }    

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
        contentPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        messageLabel = new javax.swing.JLabel();
        inputPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        usernameTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        passwordTF = new javax.swing.JPasswordField();
        jPanel3 = new javax.swing.JPanel();
        titlePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        controlPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        setMinimumSize(new java.awt.Dimension(300, 149));
        contentPanel.setLayout(new javax.swing.BoxLayout(contentPanel, javax.swing.BoxLayout.Y_AXIS));

        contentPanel.setBorder(new javax.swing.border.EtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(10, 31));
        jPanel1.setMinimumSize(new java.awt.Dimension(10, 31));
        jPanel1.setMaximumSize(new java.awt.Dimension(32767, 31));
        messageLabel.setText("Enter username and password and click OK");
        messageLabel.setToolTipText("");
        jPanel1.add(messageLabel);

        contentPanel.add(jPanel1);

        inputPanel.setMaximumSize(new java.awt.Dimension(32767, 31));
        jLabel2.setText("Username:");
        inputPanel.add(jLabel2);

        usernameTF.setText("jTextField1");
        usernameTF.setPreferredSize(new java.awt.Dimension(90, 21));
 
        inputPanel.add(usernameTF);

        jLabel3.setText("Password:");
        inputPanel.add(jLabel3);

        passwordTF.setPreferredSize(new java.awt.Dimension(90, 21));


        inputPanel.add(passwordTF);

        contentPanel.add(inputPanel);

        jPanel3.setPreferredSize(new java.awt.Dimension(10, 25));
        jPanel3.setMinimumSize(new java.awt.Dimension(10, 25));
        jPanel3.setMaximumSize(new java.awt.Dimension(32767, 2555));
        contentPanel.add(jPanel3);

        add(contentPanel, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Login");
        titlePanel.add(jLabel1);

        add(titlePanel, java.awt.BorderLayout.NORTH);

        okButton.setText("OK");
        okButton.setPreferredSize(new java.awt.Dimension(73, 27));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        controlPanel.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        controlPanel.add(cancelButton);

        add(controlPanel, java.awt.BorderLayout.SOUTH);

    }

    private void passwordTFFocusLost(java.awt.event.FocusEvent evt) {
    }

    private void usernameTFFocusLost(java.awt.event.FocusEvent evt) {
    }

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        fire(new Event(getController().getEvent(StandardController.CANCEL), this));
    }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
        char[] password =  passwordTF.getPassword();      
		fireUpdateModel(LoginModel.USERNAME_FIELD, usernameTF.getText());
		fireUpdateModel(LoginModel.PASSWORD_FIELD, new String(password));
        fire(new Event(getController().getEvent(StandardController.OK), this));
    }

    /** Allows the logger name to be set by derived class
     *
     */
    protected String getLoggerName() {
        return "srse.login.view";
    }    

    private javax.swing.JLabel messageLabel;
    private javax.swing.JPanel inputPanel;
    private javax.swing.JTextField usernameTF;
    private javax.swing.JButton okButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPasswordField passwordTF;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel titlePanel;
    
}
